package tech.aistar.corejava.day16.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/5 0005
 */
public class MatcherDemo {
    private static StringBuilder builder = new StringBuilder();

    static{
        builder.append("I Love Java,I am learning Java");
        builder.append("I Love 23 JavaScript,I am learning 45 python");
        builder.append("I Love 100 Java,I am learning Java,My tel:010-1234456");
    }

    public static void match(String regex){
        //1. 根据正则得到一个Pattern
        Pattern pattern = Pattern.compile(regex);
        //2. 得到一个匹配器
        Matcher matcher = pattern.matcher(builder);

        //3. 循环
        while(matcher.find()){//如果没有可匹配的,并且已经到达数据的末尾,返回false
            int start = matcher.start();//匹配成功的字符序列的起始位置
            int end = matcher.end();//不包括,匹配的成功的字符序列的结束的位置
            //返回满足规则的匹配成功的字符序列本身
            String group = matcher.group();

            System.out.println(start+":"+end+":"+group);


        }
    }

    public static void main(String[] args) {
        //match("\\d+");
        match("[0-9-]+");
    }
}
